/*****************************************************************************
* Keppe Messenger (KMP) Keppe Project-Team User Messaging System
* Copyright (C) 2013-2014 Keppe Project-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

#include "KMsgFw10Q.h"
#include <QFile>
#include <QSslError>
#include <QSslSocket>
#include <QStringList>
#include <QSslCertificate>

KMsgFw10Q::KMsgFw10Q(QObject *parent) :
    QObject(parent)
{
    tcpSocket = new QSslSocket();
#ifdef WIN32_SHARED
    QList<QSslCertificate> cert = QSslCertificate::fromPath(QLatin1String(":/interface.msg.keppe.org.pem"));
#else
    QList<QSslCertificate> cert = QSslCertificate::fromData("-----BEGIN CERTIFICATE-----\nMIIHXTCCBkWgAwIBAgIDEB00MA0GCSqGSIb3DQEBBQUAMIGMMQswCQYDVQQGEwJJ\nTDEWMBQGA1UEChMNU3RhcnRDb20gTHRkLjErMCkGA1UECxMiU2VjdXJlIERpZ2l0\nYWwgQ2VydGlmaWNhdGUgU2lnbmluZzE4MDYGA1UEAxMvU3RhcnRDb20gQ2xhc3Mg\nMSBQcmltYXJ5IEludGVybWVkaWF0ZSBTZXJ2ZXIgQ0EwHhcNMTQwNDI2MDAyOTM5\nWhcNMTUwNDI2MTE1NDQ4WjBqMRkwFwYDVQQNExBQOVdCQWlKOHFjbzI5ZUwyMQsw\nCQYDVQQGEwJERTEgMB4GA1UEAxMXaW50ZXJmYWNlLm1zZy5rZXBwZS5vcmcxHjAc\nBgkqhkiG9w0BCQEWD2FkbWluQGtlcHBlLm9yZzCCAiIwDQYJKoZIhvcNAQEBBQAD\nggIPADCCAgoCggIBAPBBDUnY9ebNdQl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1)
    {
        QSslError HostNameMismatch = QSslError(QSslError::HostNameMismatch, cert.at(0));
        QSslError SelfSignedCertificate = QSslError(QSslError::SelfSignedCertificate, cert.at(0));
        QSslError UnableToGetLocalIssuerCertificate = QSslError(QSslError::UnableToGetLocalIssuerCertificate, cert.at(0));
        QSslError CertificateUntrusted = QSslError(QSslError::CertificateUntrusted, cert.at(0));
        QSslError UnableToVerifyFirstCertificate = QSslError(QSslError::UnableToVerifyFirstCertificate, cert.at(0));
        QSslError CertificateExpired = QSslError(QSslError::CertificateExpired, cert.at(0));
        sslErrors.append(HostNameMismatch);
        sslErrors.append(SelfSignedCertificate);
        sslErrors.append(UnableToGetLocalIssuerCertificate);
        sslErrors.append(CertificateUntrusted);
        sslErrors.append(UnableToVerifyFirstCertificate);
        sslErrors.append(CertificateExpired);
    }
     loggedIn = false;
}

int KMsgFw10Q::loginUser(QString username, QString password)
{
    loggedIn = false;
    tcpSocket = new QSslSocket();
    tcpSocket->ignoreSslErrors(sslErrors);
    tcpSocket->connectToHostEncrypted("interface.msg.keppe.org",9506);
    bool wfe = tcpSocket->waitForEncrypted(5000);
    if (wfe)
    {
        tcpSocket->write("SEND /login KM/1.0\n");
        tcpSocket->write("Username: ");
        tcpSocket->write(username.toUtf8());
        tcpSocket->write("\nPassword: ");
        tcpSocket->write(password.toUtf8());
        tcpSocket->write("\n");
        tcpSocket->flush();
        tcpSocket->waitForReadyRead(5000);
        while (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            if (readstr.right(6) == "200 OK")
            {
                loggedIn = true;
                if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                return 200;
            }
            else if (readstr.right(6) == "404 NF")
            {
                if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                return 404;
            }
            else if (readstr.right(13) == "404 Not Found")
            {
                if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                return 404;
            }
            else
            {
                if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                QStringList strlist = readstr.split(" ");
                if (strlist.length() >= 2)
                {
                    return QVariant(strlist.at(1)).toInt();
                }
                else
                {
                    return 502;
                }
            }
        }
    }
    return 500;
}

int KMsgFw10Q::logoutUser()
{
    tcpSocket->disconnectFromHost();
    loggedIn = false;
    return 0;
}

bool KMsgFw10Q::isLoggedIn()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            return true;
        }
    }
    return false;
}

QString KMsgFw10Q::getMessengerStandardAvatar()
{
    QFile *file = new QFile();
    file->setFileName(":/avatar/km-standard");
    file->open(QFile::ReadOnly);
    QByteArray qa = file->readAll();
    QString qs = QString::fromUtf8(qa);
    file->close();
    return qs;
}

QString KMsgFw10Q::getUserContacts()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /getusercontacts KM/1.0\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            if (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    QString table;
                    while (tcpSocket->canReadLine())
                    {
                        QByteArray rr = tcpSocket->readLine().trimmed();
                        QString rs = QString::fromUtf8(rr);
                        if (rs.left(7) == "Users: ")
                        {
                            QString users = rs.remove(0,7);
                            table = users;
                            table = table.replace("<>","\n");
                            return table;
                        }

                    }
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return "///Error 201";
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return "///Error 500";
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QString("///Error " + strlist.at(1));
                    }
                    else
                    {
                        return "///Error 502";
                    }
                }
            }
        }
    }
    return "///Error 1";
}

QString KMsgFw10Q::getUserContactsLength()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /getusercontactslength KM/1.0\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            if (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    while (tcpSocket->canReadLine())
                    {
                        QByteArray rr = tcpSocket->readLine().trimmed();
                        QString rs = QString::fromUtf8(rr);
                        if (rs.left(8) == "Length: ")
                        {
                            QString length = rs.remove(0,8);
                            return length;
                        }

                    }
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return "///Error 201";
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return "///Error 500";
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QString("///Error " + strlist.at(1));
                    }
                    else
                    {
                        return "///Error 502";
                    }
                }
            }
        }
    }
    return "///Error 1";
}

QString KMsgFw10Q::getUserContactsRequests()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /getuserfriendrequests KM/1.0\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            if (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    QString table;
                    while (tcpSocket->canReadLine())
                    {
                        QByteArray rr = tcpSocket->readLine().trimmed();
                        QString rs = QString::fromUtf8(rr);
                        if (rs.left(7) == "Users: ")
                        {
                            QString users = rs.remove(0,7);
                            table = users;
                            table = table.replace("<>","\n");
                            return table;
                        }

                    }
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return "///Error 201";
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return "///Error 500";
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QString("///Error " + strlist.at(1));
                    }
                    else
                    {
                        return "///Error 502";
                    }
                }
            }
        }
    }
    return "///Error 1";
}

QString KMsgFw10Q::getUserContactsWithStates()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /getusercontactswithstates KM/1.0\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            if (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    QString table;
                    while (tcpSocket->canReadLine())
                    {
                        QByteArray rr = tcpSocket->readLine().trimmed();
                        QString rs = QString::fromUtf8(rr);
                        if (rs.left(7) == "Users: ")
                        {
                            QString users = rs.remove(0,7);
                            table = users;
                            table = table.replace("<>","\n");
                            return table;
                        }

                    }
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return "///Error 201";
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return "///Error 500";
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QString("///Error " + strlist.at(1));
                    }
                    else
                    {
                        return "///Error 502";
                    }
                }
            }
        }
    }
    return "///Error 1";
}

QString KMsgFw10Q::getUserFriendRequests()
{
    return getUserContactsRequests();
}

QString KMsgFw10Q::getUserFriendRequestsLength()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /getuserfriendrequestslength KM/1.0\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            if (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    while (tcpSocket->canReadLine())
                    {
                        QByteArray rr = tcpSocket->readLine().trimmed();
                        QString rs = QString::fromUtf8(rr);
                        if (rs.left(8) == "Length: ")
                        {
                            QString length = rs.remove(0,8);
                            return length;
                        }

                    }
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return "///Error 201";
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return "///Error 500";
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QString("///Error " + strlist.at(1));
                    }
                    else
                    {
                        return "///Error 502";
                    }
                }
            }
        }
    }
    return "///Error 1";
}

int KMsgFw10Q::addUserContact(QString contact)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /addusercontact KM/1.0\n");
            tcpSocket->write("Username: ");
            tcpSocket->write(contact.toUtf8());
            tcpSocket->write("\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            while (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 200;
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 500;
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QVariant(strlist.at(1)).toInt();
                    }
                    else
                    {
                        return 502;
                    }
                }
            }
        }
    }
    return 1;
}

int KMsgFw10Q::delUserContact(QString contact)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /removeusercontact KM/1.0\n");
            tcpSocket->write("Username: ");
            tcpSocket->write(contact.toUtf8());
            tcpSocket->write("\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            while (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 200;
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 500;
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QVariant(strlist.at(1)).toInt();
                    }
                    else
                    {
                        return 502;
                    }
                }
            }
        }
    }
    return 1;
}

int KMsgFw10Q::removeUserContact(QString contact)
{
    return delUserContact(contact);
}

int KMsgFw10Q::getUserOnlineState()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /getuseronlinestate KM/1.0\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            if (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    if (tcpSocket->canReadLine())
                    {
                        QByteArray rr = tcpSocket->readLine().trimmed();
                        QString rs = QString::fromUtf8(rr);
                        if (rs.left(7) == "State: ")
                        {
                            QString state = rs.remove(0,7);
                            if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                            return QVariant(state).toInt();
                        }
                    }
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 201;
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 500;
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QVariant(strlist.at(1)).toInt();
                    }
                    else
                    {
                        return 502;
                    }
                }
            }
        }
    }
    return 501;
}

int KMsgFw10Q::setUserOnlineState(int state)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /setuseronlinestate KM/1.0\n");
            tcpSocket->write("State: ");
            tcpSocket->write(QString::number(state).toUtf8());
            tcpSocket->write("\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            while (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    loggedIn = true;
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 200;
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 500;
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QVariant(strlist.at(1)).toInt();
                    }
                    else
                    {
                        return 502;
                    }
                }
            }
        }
    }
    return 1;
}

QString KMsgFw10Q::getUserMessageState()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /getusermessagestate KM/1.0\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            if (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    if (tcpSocket->canReadLine())
                    {
                        QByteArray rr = tcpSocket->readLine().trimmed();
                        QString rs = QString::fromUtf8(rr);
                        if (rs.left(9) == "Message: ")
                        {
                            QString messagestate = rs.remove(0,9);
                            if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                            return messagestate;
                        }
                    }
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return "///Error 201";
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return "///Error 500";
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QString("///Error " + strlist.at(1));
                    }
                    else
                    {
                        return "///Error 502";
                    }
                }
            }
        }
    }
    return "///Error 1";
}

int KMsgFw10Q::setUserMessageState(QString message)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /setusermessagestate KM/1.0\n");
            tcpSocket->write("Message: ");
            tcpSocket->write(message.toUtf8());
            tcpSocket->write("\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            while (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 200;
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 500;
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QVariant(strlist.at(1)).toInt();
                    }
                    else
                    {
                        return 502;
                    }
                }
            }
        }
    }
    return 1;
}

int KMsgFw10Q::setUserAvatar(QString base64, QString format)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /setuseravatar KM/1.0\n");
            tcpSocket->write("Avatar: ");
            tcpSocket->write(base64.toUtf8());
            tcpSocket->write("\n");
            tcpSocket->write("Format: ");
            tcpSocket->write(format.toUtf8());
            tcpSocket->write("\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            while (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 200;
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 500;
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QVariant(strlist.at(1)).toInt();
                    }
                    else
                    {
                        return 502;
                    }
                }
            }
        }
    }
    return 1;
}

QString KMsgFw10Q::getUserNick()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /getusernick KM/1.0\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            if (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    if (tcpSocket->canReadLine())
                    {
                        QByteArray rr = tcpSocket->readLine().trimmed();
                        QString rs = QString::fromUtf8(rr);
                        if (rs.left(6) == "Nick: ")
                        {
                            QString nick = rs.remove(0,6);
                            if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                            return nick;
                        }
                    }
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return "///Error 201";
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return "///Error 500";
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QString("///Error " + strlist.at(1));
                    }
                    else
                    {
                        return "///Error 502";
                    }
                }
            }
        }
    }
    return "///Error 1";
}

int KMsgFw10Q::setUserNick(QString nick)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /setusernick KM/1.0\n");
            tcpSocket->write("Nick: ");
            tcpSocket->write(nick.toUtf8());
            tcpSocket->write("\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            while (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 200;
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 500;
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QVariant(strlist.at(1)).toInt();
                    }
                    else
                    {
                        return 502;
                    }
                }
            }
        }
    }
    return 1;
}

int KMsgFw10Q::setUserMail(QString mail, bool vfo)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /setusermail KM/1.0\n");
            tcpSocket->write("Mail: ");
            tcpSocket->write(mail.toUtf8());
            tcpSocket->write("\n");
            tcpSocket->write("VFO: ");
            tcpSocket->write(QVariant(vfo).toString().toUtf8());
            tcpSocket->write("\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            while (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 200;
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 500;
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QVariant(strlist.at(1)).toInt();
                    }
                    else
                    {
                        return 502;
                    }
                }
            }
        }
    }
    return 1;
}

int KMsgFw10Q::setUserPassword(QString oldpassword, QString newpassword)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /setuserpassword KM/1.0\n");
            tcpSocket->write("Password old: ");
            tcpSocket->write(oldpassword.toUtf8());
            tcpSocket->write("\nPassword new: ");
            tcpSocket->write(newpassword.toUtf8());
            tcpSocket->write("\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            while (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 200;
                }
                else if (readstr.right(6) == "500 Error")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 500;
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QVariant(strlist.at(1)).toInt();
                    }
                    else
                    {
                        return 502;
                    }
                }
            }
        }
    }
    return 1;
}

int KMsgFw10Q::sendMessageToUser(QString username, QString message)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /message KM/1.0\n");
            tcpSocket->write("Username: ");
            tcpSocket->write(username.toUtf8());
            tcpSocket->write("\nMessage: ");
            tcpSocket->write(message.toUtf8());
            tcpSocket->write("\n");
            tcpSocket->flush();
            tcpSocket->waitForReadyRead(5000);
            while (tcpSocket->canReadLine())
            {
                QByteArray readed = tcpSocket->readLine().trimmed();
                QString readstr = QString::fromUtf8(readed);
                if (readstr.right(6) == "200 OK")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 200;
                }
                else if (readstr.right(6) == "404 Not Found")
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    return 404;
                }
                else
                {
                    if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
                    QStringList strlist = readstr.split(" ");
                    if (strlist.length() >= 2)
                    {
                        return QVariant(strlist.at(1)).toInt();
                    }
                    else
                    {
                        return 502;
                    }
                }
            }
        }
    }
    return 1;
}

int KMsgFw10Q::sendPokeToUser(QString username)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == tcpSocket->ConnectedState)
        {
            tcpSocket->write("SEND /poke KM/1.0\n");
            tcpSocket->write("Username: ");
            tcpSocket->write(username.toUtf8());
            tcpSocket->write("\n");
            tcpSocket->flush();
            if (tcpSocket->canReadLine()) {tcpSocket->readAll();}
            return 0;
        }
    }
    return 1;
}

bool KMsgFw10Q::isObjectIncoming()
{
    return tcpSocket->canReadLine();
}

QString KMsgFw10Q::readObject()
{
    if (!tcpSocket->canReadLine())
    {
        return "";
    }
    QByteArray readed = tcpSocket->readLine().trimmed();
    QString readstr = QString::fromUtf8(readed);
    QString command;
    if (readstr.length() >= 2)
    {
        command = readstr.split(" ").at(1);
    }
    if (command == "/message")
    {
        QString username;
        QString message;
        while (tcpSocket->canReadLine())
        {
            QByteArray rr = tcpSocket->readLine().trimmed();
            QString rs = QString::fromUtf8(rr);
            if (rs.left(10) == "Username: ")
            {
                username = rs.remove(0,10);
            }
            if (rs.left(9) == "Message: ")
            {
                message = rs.remove(0,9);
            }
        }
        if (username == "" || message == "")
        {
            return "";
        }
        return QString("MessageFromUser<>" + username + ": " + message + "\n");
    }
    if (command == "/poke")
    {
        QString username;
        while (tcpSocket->canReadLine())
        {
            QByteArray rr = tcpSocket->readLine().trimmed();
            QString rs = QString::fromUtf8(rr);
            if (rs.left(10) == "Username: ")
            {
                username = rs.remove(0,10);
            }
        }
        if (username == "")
        {
            return "";
        }
        return QString("PokeFromUser<>" + username + "\n");
    }
    if (command == "/update")
    {
        return "update";
    }
    if (command == "/ping")
    {
        tcpSocket->write("KM/1.0 200 OK\nPing: Pong\n");
        tcpSocket->flush();
        return "ping/pong";
    }
    return "";
}

QString KMsgFw10Q::getClassVersion()
{
    return "1.0.0-5";
}
